<?php defined('InNcPHP') or exit('Access Invalid!');?>

<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <h3><?php echo $lang['ticket_index_ticket'];?></h3>
            <?php echo $output['top_link'];?>
        </div>
    </div>
    <div class="fixed-empty"></div>
    <!--  查询表单开始  -->
    <form method="get" name="formSearch" id="formSearch">
        <input type="hidden" name="act" value="ticket">
        <input type="hidden" name="op" value="index">
        <table class="tb-type1 noborder search">
            <tbody>
            <tr>
                <th><label for="search_title"> <?php echo $lang['ticket_index_title'];?></label></th>
                <td><input type="text" name="search_title" value="<?php echo $output['search']['search_title']; ?>" id="search_title" class="txt"></td>
                <th><label><?php echo $lang['ticket_index_status'];?></label></th>
                <td>
                    <select name="search_state">
                        <option value=""><?php echo $lang['nc_please_choose'];?>...</option>
                        <option value="1" <?php echo $output['search']['search_state'] === '1' ? 'selected' : ''; ?>><?php echo $lang['ticket_index_show'];?></option>
                        <option value="0" <?php echo $output['search']['search_state'] === '0' ? 'selected' : ''; ?>><?php echo $lang['ticket_index_hide'];?></option>
                    </select>
                </td>
                <th><label><?php echo $lang['ticket_index_class'];?></label></th>
                <td>
                    <select name="search_cate1" id="cate1">
                        <option value=""><?php echo $lang['nc_please_choose'];?>...</option>
                        <?php if (is_array($output['cate1'])): ?>
                        <?php foreach ($output['cate1'] as $k => $v): ?>
                        <option value="<?php echo $v['tc_id']; ?>" <?php echo $output['search']['search_cate1'] == $v['tc_id'] ? 'selected' : ''; ?>><?php echo $v['tc_name']; ?></option>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </select><!--
                    <select name="search_cate2" id="cate2">
                        <option value=""><?php /*echo $lang['nc_please_choose'];*/?>...</option>
                        <?php /*if ($output['defaultCate2'] && is_array($output['defaultCate2'])): */?>
                        <?php /*foreach ($output['defaultCate2'] as $k => $v): */?>
                        <option value="<?php /*echo $v['tc_id']; */?>" <?php /*echo $output['search']['search_cate2'] == $v['tc_id'] ? 'selected' : ''; */?>><?php /*echo $v['tc_name']; */?></option>
                        <?php /*endforeach; */?>
                        <?php /*endif; */?>
                    </select>-->
                </td>
                <th><label><?php echo $lang['ticket_index_merchant'];?></label></th>
                <td>
                    <select name="search_merchant">
                        <option value=""><?php echo $lang['nc_please_choose'];?>...</option>
                        <?php if (is_array($output['merchants'])): ?>
                        <?php foreach ($output['merchants'] as $k => $v): ?>
                        <option value="<?php echo $v['merchant_id']; ?>" <?php echo $output['search']['search_merchant'] == $v['merchant_id'] ? 'selected' : ''; ?>><?php echo $v['mc_shop_name']; ?></option>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </td>
                <th></th>
                <td><a href="javascript:void(0);" onclick="$('#formSearch').submit();" class="btn-search"></a></td>
            </tr>
            </tbody>
        </table>
    </form>
    <!--  操作提示  -->
    <table class="table tb-type2" id="prompt">
        <tbody>
        <tr class="space odd">
            <th colspan="12">
                <div class="title">
                    <h5><?php echo $lang['nc_prompts']; ?></h5>
                    <span class="arrow"></span>
                </div>
            </th>
        </tr>
        <tr>
            <td>
                <ul>
                    <li><?php echo $lang['ticket_index_help']; ?></li>
                </ul>
            </td>
        </tr>
        </tbody>
    </table>

    <a class="m-btn blue-stripe mini <?php echo ($_GET['search_state']=='')?'checked':'' ?> " onclick="parent.openItem('index,ticket,ticket');" href="#">全部门票</a>
    <a class="m-btn yellow-stripe mini  <?php echo ($_GET['search_state']==1)?'checked':'' ?> " onclick="parent.openItem('index,ticket,ticket,search_state=1');" href="#">上架</a>
    <a class="m-btn red-stripe mini <?php echo ($_GET['search_state']=='0')?'checked':'' ?>" onclick="parent.openItem('index,ticket,ticket,search_state=0');"  href="#">下架</a>


    <!--  门票列表  -->
    <form method='post' onsubmit="return false;">
        <input type="hidden" name="form_submit" value="ok" />
        <table class="table tb-type2">
            <thead>
            <tr class="thead">
                <th class="w24"></th>
                <th colspan="2"><?php echo $lang['ticket_index_ticket']; ?></th>
                <th><?php echo $lang['ticket_index_class']; ?></th>
                <th>SKU</th>
                <th class="align-center"><?php echo $lang['ticket_add_show_price']; ?></th>
                <th class="align-center"><?php echo $lang['ticket_add_market_price']; ?></th>
               <!-- <th class="align-center"><?php /*echo $lang['ticket_add_discount']; */?></th>-->
                <th class="align-center"><?php echo $lang['ticket_index_status']; ?></th>
                <th class="align-center"><?php echo $lang['ticket_index_addtime']; ?></th>
               <!-- <th class="align-center"><?php /*echo $lang['ticket_add_recommend']; */?></th>-->
                <th class="w96 align-center"><?php echo $lang['nc_handle']; ?></th>
            </tr>
            </thead>
            <tbody>
            <?php if (is_array($output['tickets'])) { ?>
                <?php foreach ($output['tickets'] as $k => $v) { ?>
                    <tr class="hover edit">
                        <td><input type="checkbox" name="tk_id[]" value="<?php echo $v['tk_id']; ?>" class="checkitem"></td>
                        <td class="w60 picture">
                            <div class="size-56x56">
                                <span class="thumb size-56x56"><img style="max-height: 56px;max-width: 56px;" src="<?php echo $v['tk_thumb']; ?>" /></span>
                            </div>
                        </td>
                        <td class="w150">
                            <p><span><?php echo $v['tk_title']; ?></span></p>
                            <p class="store"><?php echo $lang['ticket_index_merchant']; ?>:<?php echo $v['shop_name']; ?></p>
                        </td>
                        <td><?php echo $v['tk_cate1']; ?> / <?php echo $v['tk_cate2']; ?></td>
                        <td>
                            <?php if ($v['tk_type'] == 0): ?>
                                <?php foreach ($v['sku'] as $sku): ?>
                                    <p><?php echo $sku['type_name']; ?>/<?php echo $lang['ticket_index_store'].':'.$sku['sku_store'].'/'.$lang['ticket_index_price'].':'.$lang['currency'].$sku['sku_price']; ?></p>
                                <?php endforeach; ?>

                            <?php elseif ($v['tk_type'] == 1): ?>
                            <a href="index.php?act=ticket&op=days&tk_id=<?php echo $v['tk_id']; ?>"><?php echo $lang['ticket_index_date']; ?></a>
                            <?php elseif ($v['tk_type'] == 2): ?>
                                <?php echo $lang['ticket_add_type_none']; ?>
                            <?php endif; ?>
                        </td>
                        <td class="align-center"><?php echo $lang['currency'].$v['tk_show_price']; ?></td>
                        <td class="align-center"><?php echo $lang['currency'].$v['tk_market_price']; ?></td>
                        <!--<td class="align-center"><?php /*echo $v['tk_is_discount'] ? $lang['nc_yes'] : $lang['nc_no']; */?></td>-->
                        <td class="align-center"><?php echo $v['tk_status'] ? $lang['ticket_index_show'] : $lang['ticket_index_hide']; ?></td>
                        <td class="align-center"><?php echo date('Y-m-d H:i:s',$v['tk_add_time']); ?></td>
                        <!--<td class="align-center yes-onoff">
                            <a href="JavaScript:void(0);" onclick="changeRecommend(this,<?php /*echo $v['tk_id']; */?>);" class="<?php /*echo $v['tk_is_recommend'] ? 'enabled' : 'disabled'; */?>">
                                <img src="<?php /*echo ADMIN_TEMPLATES_URL; */?>/images/transparent.gif">
                            </a>
                        </td>-->
                        <td class="align-center w96">
                            <p>
                                <a href="index.php?act=ticket&op=edit&tk_id=<?php echo $v['tk_id']; ?>"><?php echo $lang['nc_edit']; ?></a> |
                              <?php if($output['admin_info']['merchant_id']==0):?>
                                <a href="javascript:void(0);" onclick="changeStatus(this,<?php echo $v['tk_id']; ?>);"><?php echo $v['tk_status'] ? $lang['ticket_index_hide'] : $lang['ticket_index_show']; ?></a>
                              <?php if($output['admin_info']['id']==1){ ?>
                                  |
                                <a href="javascript:void(0);" style="color:red;" onclick="delGoods(this,<?php echo $v['tk_id']; ?>);">删除</a>
                               <?php } ?>
                              <?php endif; ?>
                                <!--|
                                <a href="###" class="copylink" data-clipboard-text="<?php /*echo C('wx_site_url') . '/index.php?act=ticket&op=detail&tid=' . $v['tk_id']; */?>">复制</a>-->
                            </p>
                        </td>
                    </tr>
                <?php } ?>
            <?php } else { ?>
                <tr class="no_data">
                    <td colspan="15"><?php echo $lang['nc_no_record']; ?></td>
                </tr>
            <?php } ?>
            </tbody>
            <?php if($output['admin_info']['merchant_id']==0):?>
            <tfoot>
            <tr class="tfoot">
                <td><input type="checkbox" class="checkall" id="checkallBottom"></td>
                <td colspan="16">
                    <label for="checkallBottom"><?php echo $lang['nc_select_all']; ?></label>
                    <a href="JavaScript:void(0);" class="btn" onclick="changeAll('hide');"><span><?php echo $lang['ticket_index_hide']; ?></span></a>
                    <a href="JavaScript:void(0);" class="btn" onclick="changeAll('show');"><span><?php echo $lang['ticket_index_show']; ?></span></a>
                    <div class="pagination"> <?php echo $output['page']; ?></div>
                </td>
            </tr>
            </tfoot>
            <?php endif; ?>
        </table>
    </form>
</div>

<script src="resource/js/clipboard.min.js"></script>
<script>
    //复制链接
    var clipboard = new Clipboard('.copylink');
    clipboard.on('success', function(e) {
        layer.alert('复制成功');
    });

    //ajax请求登录过期重定向
    $(document).ajaxError(function(event,request, settings){
        location.reload();
    });

    //选择一级分类
    $("#cate1").change(function(){
        var _tcId = $(this).val();
        _this = $(this);
        if(_tcId){
            $.getJSON("index.php?act=common&op=getCategory&tc_pid=" + _tcId, function(data){
                $obj = $("#cate2");
                $obj.empty();
                $obj.append('<option value=""><?php echo $lang['nc_please_choose'];?>...</option>');
                $.each(data, function(i,item){
                    $obj.append('<option value="'+item.tc_id+'">'+item.tc_name+'</option>');
                });
            });
        }else{
            _this.next("select").empty();
            _this.next("select").append('<option value=""><?php echo $lang['nc_please_choose'];?>...</option>');
        }
    });

    //更改推荐状态
    function changeRecommend(obj, id)
    {
        var _this = $(obj);
        $.post('index.php?act=ticket&op=ajax&type=change_recommend', {tk_id: id}, function (data) {
            if (data.res) {
                _this.attr('class', data.new_recommend ? 'enabled' : 'disabled');
            }
        }, 'json');
    }

    //更改上架状态
    function changeStatus(obj, id)
    {
        var _this = $(obj);
        layer.confirm('<?php echo $lang['ticket_index_confirm']; ?> ' + _this.text() + ' ？', {
            btn: ['<?php echo $lang['nc_yes']; ?>', '<?php echo $lang['nc_no']; ?>']
        }, function (index) {
            $.post('index.php?act=ticket&op=ajax&type=change_status', {tk_id: id}, function (data) {
                if (data.res) {
                    var handle = data.new_status ? '<?php echo $lang['ticket_index_hide']; ?>' : '<?php echo $lang['ticket_index_show']; ?>';
                    var status = data.new_status ? '<?php echo $lang['ticket_index_show']; ?>' : '<?php echo $lang['ticket_index_hide']; ?>';
                    _this.text(handle);
                    _this.parent().parent().prevAll().eq(1).text(status);
                }else{
                    layer.alert(data.msg_)
                }
                layer.close(index);
            }, 'json');
        });
    }

    //选择上下架
    function changeAll(type)
    {
        var inputs = $('input[name="tk_id[]"]:checked')
        var ids = [];
        inputs.each(function (i) {
            ids.push(this.value);
        });
        //校验选项是否为空
        if (ids.length < 1) {
            layer.alert('<?php echo $lang['ticket_index_choose_null']; ?>');
            return;
        }
        //下架所有选择的商品
        var confirm = type == 'show' ? '<?php echo $lang['ticket_index_confirm_show']; ?>？' : '<?php echo $lang['ticket_index_confirm_hide']; ?>？';
        var showText = '<?php echo $lang['ticket_index_show']; ?>';
        var hideText = '<?php echo $lang['ticket_index_hide']; ?>';
        layer.confirm(confirm, {
            btn: ['<?php echo $lang['nc_yes']; ?>', '<?php echo $lang['nc_no']; ?>']
        }, function (index) {
            $.post('index.php?act=ticket&op=ajax&type=change_all', {ids: ids, type: type}, function (data) {
                if (data.res) {
                    inputs.each(function (i) {
                        var _next = $(this).parent().nextAll();
                        _next.eq(7).text(type == 'show' ? showText : hideText);
                        _next.eq(10).find('a').eq(1).text(type == 'show' ? hideText : showText);
                    });
                }else{
                    layer.alert(data.msg_)
                }
                layer.close(index);
            }, 'json');
        });
    }

    //删除商品
    function delGoods(obj,id)
    {
        var _this = $(obj);
        layer.confirm('确定要删除该商品?请您谨慎操作,一旦删除数据将无法恢复！', {
            btn: ['<?php echo $lang['nc_yes']; ?>', '<?php echo $lang['nc_no']; ?>']
        }, function (index) {
            $.post('index.php?act=ticket&op=del_ticket', {tk_id: id}, function (data) {
               if(data.res){
                   layer.alert('删除成功', function () {
                       location.reload();
                   });
               }else{
                   layer.alert(data.msg_)
               }
                layer.close(index);
            }, 'json');
        });
    }
</script>
